package com.zj.fx.service.info;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import com.zj.fx.mapper.auto.SysDepartmentMapper;
import com.zj.fx.mapper.auto.TsysUserMapper;
import com.zj.fx.mapper.info.InfoSupplyMapper;
import com.zj.fx.model.auto.SysDepartment;
import com.zj.fx.model.auto.TsysRole;
import com.zj.fx.model.auto.TsysRoleExample;
import com.zj.fx.model.auto.TsysUser;
import com.zj.fx.model.custom.DepVo;
import com.zj.fx.model.custom.InfoSupplyVo;
import com.zj.fx.model.info.InfoSupply;
import com.zj.fx.model.info.InfoSupplyExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.util.StrUtil;
import com.zj.fx.common.base.BaseService;
import com.zj.fx.common.support.ConvertUtil;
import com.zj.fx.model.custom.Tablepar;
import com.zj.fx.util.SnowflakeIdWorker;
import com.zj.fx.util.StringUtils;

/**
 * 供电所信息表 InfoSupplyService
 *
 * @author fuce_自动生成
 * @Title: InfoSupplyService.java 
 * @Package com.zj.fx.service 
 * @email ${email}
 * @date 2023-06-06 14:27:04  
 **/
@Service
public class InfoSupplyService implements BaseService<InfoSupply, InfoSupplyExample> {
    @Autowired
    private InfoSupplyMapper infoSupplyMapper;

    @Autowired
    private TsysUserMapper tsysUserMapper;

    /**
     * 分页查询
     *
     * @param
     * @param
     * @return
     */
    public PageInfo<InfoSupply> list(Tablepar tablepar, InfoSupply infoSupply) {
        InfoSupplyExample testExample = new InfoSupplyExample();
        //搜索
        if (StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
            testExample.createCriteria().andLikeQuery2(tablepar.getSearchText());
        } else {//大搜索
            testExample.createCriteria().andLikeQuery(infoSupply);
        }
        //表格排序
        if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
        	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
        }else{
        	testExample.setOrderByClause("NUM+0 ASC");
        }
        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
        List<InfoSupply> list = infoSupplyMapper.selectByExample(testExample);
        PageInfo<InfoSupply> pageInfo = new PageInfo<InfoSupply>(list);
        return pageInfo;
    }

    @Override
    public int deleteByPrimaryKey(String ids) {

        List<String> lista = ConvertUtil.toListStrArray(ids);
        InfoSupplyExample example = new InfoSupplyExample();
        example.createCriteria().andIdIn(lista);
        return infoSupplyMapper.deleteByExample(example);


    }


    @Override
    public InfoSupply selectByPrimaryKey(String id) {

        return infoSupplyMapper.selectByPrimaryKey(id);

    }


    @Override
    public int updateByPrimaryKeySelective(InfoSupply record) {
        return infoSupplyMapper.updateByPrimaryKeySelective(record);
    }


    /**
     * 添加
     */
    @Override
    public int insertSelective(InfoSupply record) {

        //添加雪花主键id
        record.setId(SnowflakeIdWorker.getUUID());


        return infoSupplyMapper.insertSelective(record);
    }


    @Override
    public int updateByExampleSelective(InfoSupply record, InfoSupplyExample example) {

        return infoSupplyMapper.updateByExampleSelective(record, example);
    }


    @Override
    public int updateByExample(InfoSupply record, InfoSupplyExample example) {

        return infoSupplyMapper.updateByExample(record, example);
    }

    @Override
    public List<InfoSupply> selectByExample(InfoSupplyExample example) {

        return infoSupplyMapper.selectByExample(example);
    }


    @Override
    public long countByExample(InfoSupplyExample example) {

        return infoSupplyMapper.countByExample(example);
    }


    @Override
    public int deleteByExample(InfoSupplyExample example) {

        return infoSupplyMapper.deleteByExample(example);
    }

    /**
     * 查询全部供电所集合
     *
     * @return
     */
    public List<InfoSupply> queryList() {
        InfoSupplyExample infoSupplyExample = new InfoSupplyExample();
        return infoSupplyMapper.selectByExample(infoSupplyExample);
    }

    /**
     * 获取所有部门 并且增加是否有部门字段
     *
     * @return
     */
    public List<InfoSupplyVo> getUserIsSupply(String userid) {
        List<InfoSupplyVo> list = new ArrayList<InfoSupplyVo>();
        //查询出我的部门
        TsysUser user = tsysUserMapper.selectByPrimaryKey(userid);
        //查询系统所有的部门
        List<InfoSupply> supplyList = this.queryList();
        if (StringUtils.isNotEmpty(supplyList)) {
            for (InfoSupply infoSupply : supplyList) {
                Boolean isflag = false;
                InfoSupplyVo infoSupplyVo = new InfoSupplyVo(infoSupply.getId(), infoSupply.getName(), isflag);
                if (user.getSupplyId()!=null){
                    String[] supplyIdArr = user.getSupplyId().split(",");
                    if (supplyIdArr.length > 0) {
                        for (int i = 0; i < supplyIdArr.length; i++) {
                            if (String.valueOf(infoSupply.getId()).equals(supplyIdArr[i])) {
                                isflag = true;
                                break;
                            }
                        }
                    }
                }
                if (isflag) {
                    infoSupplyVo.setIscheck(true);
                    list.add(infoSupplyVo);
                } else {
                    list.add(infoSupplyVo);
                }
            }
        }
        return list;
    }
}
